package org.aplikator.client.widgets;

import org.aplikator.client.Aplikator.AplikatorResources;
import org.aplikator.client.descriptor.PropertyDTO;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.CheckBox;

public class CheckBoxWidget extends DataFieldBase<Boolean> implements DataField<Boolean> {
    private CheckBox box = new CheckBox();

    public CheckBoxWidget(String caption, PropertyDTO<Boolean> property, boolean labelAbove) {
        super(caption, property, false, labelAbove);
        add(box);
        box.addStyleName(PaneWidgetResources.INSTANCE.css().formData());
        box.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                setDirty(true);
            }
        });
    }

    public void setValue(Boolean value) {
        box.setValue(value, false);
    }

    public Boolean getValue() {
        return box.getValue();
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Boolean> handler) {
        return box.addValueChangeHandler(handler);
    }

    public void setEnabled(boolean enabled) {
        box.setEnabled(enabled);
    }

    public void setDirty(boolean dirty) {
        super.setDirty(dirty);
        if (dirty) {
            box.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            box.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

}
